#include <sys/types.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

#ifdef _WIN32
extern int getopt(int, char * const *, const char *);
#else
#include <unistd.h>
#endif

#include "config.h"

int usage(const char *progname);
char *progname = "rdb_test";

int g_num = 1;
char* g_prefix = 0;
char* g_db = "/tmp/db1";
char* g_sock = "/tmp/sock";
int g_over = 0;
char* g_key = "hello";
int g_reply = 1;
int g_sleep = 0;
int g_stat = 0;
int g_showtime = 0;

int test_rdb_put_array();
int test_rdb_put();
int test_rdb_get_array();
int test_rdb_get();
int test_rdb_del_array();
int test_rdb_del();
int test_rdb_size();

int main(int argc, char *argv[])
{
	int act = 0;
	struct timeval t1,t2;
	double timeuse;

	int c;
	const char* shortopts = "f:P:p:oG:g:D:d:n:rs:t";	

	if (argc == 1)
		return usage(progname);
	while ( (c = getopt(argc, argv, shortopts)) != EOF) {
		switch (c) {
		case 'P':
			g_prefix = strdup(optarg);
			act = c;
			break;
		case 'p':
			g_key = strdup(optarg);
			act = c;
			break;
		case 'o':
			g_over = 1;
			break;
		case 'G':
			g_prefix = strdup(optarg);
			act = c;
			break;
		case 'g':
			g_key = strdup(optarg);
			act = c;
			break;
		case 'D':
			g_prefix = strdup(optarg);
			act = c;
			break;
		case 'd':
			g_key = strdup(optarg);
			act = c;
			break;
		case 'n':
			g_num = atoi(optarg);
			break;
		case 'f':
			g_db = strdup(optarg);
			break;
		case 'r':
			g_reply = 0;
			break;
		case 's':
			g_stat = 1;
			act = c;
			break;
        case 't':
            g_showtime = 1;
            break;
		case '?':
			return usage(progname);
		}
	}
	argc -= optind;
	argv += optind;
		
	gettimeofday(&t1, 0);
	switch (act) {
	case 'P': test_rdb_put_array(); break;
	case 'p': test_rdb_put(); break;
	case 'G': test_rdb_get_array(); break;
	case 'g': test_rdb_get(); break;
	case 'D': test_rdb_del_array(); break;
	case 'd': test_rdb_del(); break;	
	case 's': test_rdb_size(); break;
	default: 
		return usage(progname);
	}
	if (g_showtime){
        gettimeofday(&t2, 0);
        timeuse = (t2.tv_sec - t1.tv_sec)*1000000+ t2.tv_usec - t1.tv_usec;
        timeuse = timeuse / 1000000;
        printf("\ntime sec=%f\n", timeuse);
    }

	return 0;
}

int usage(const char *progname)
{
	fprintf(stderr, "usage: %s [-f database] [-P prefix] [-p key] [-o] [-G prefix] [-g key] "
		"[-D prefix] [-d key] [-n nums] [-r] [-s]\n", progname);
	fprintf(stderr, " -f database: database's name\n");
	fprintf(stderr, " -P prefix: Put multi-datas. specify key's prefix name\n");
	fprintf(stderr, " -p key: put data with specify key\n");
	fprintf(stderr, " -o: put data with overwrite\n");
	fprintf(stderr, " -G prefix: Get multi-datas. specify key's prefix name\n");
	fprintf(stderr, " -g key: get data with specify key\n");
	fprintf(stderr, " -D prefix: Delete multi-datas. specify key's prefix name\n");
	fprintf(stderr, " -d key: delete data with specify key\n");
	fprintf(stderr, " -n nums: Put or Get n datas\n");
	fprintf(stderr, " -r: set option noreply\n");
	fprintf(stderr, " -s: get db record size\n");
	fprintf(stderr, " -t: show program's time and other info\n");
	fprintf(stderr, "\n");
	return 1;
}

